package com.tarena.lbs.basic.web.service.impl;

import com.tarena.lbs.attach.api.AttachApi;
import com.tarena.lbs.attach.api.UpdateParam;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.repoistory.BusinessRepository;
import com.tarena.lbs.basic.web.service.BusinessService;
import com.tarena.lbs.common.basic.enums.BusStatusEnum;
import com.tarena.lbs.pojo.basic.bo.BusinessBO;
import com.tarena.lbs.pojo.basic.param.BusinessParam;
import com.tarena.lbs.pojo.basic.query.BusinessQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private AttachApi attachApi;


    @Override
    public BasePageList<BusinessBO> pageList(BusinessQuery businessQuery) {
        //调用仓储层  给我根据条件进行查询. 持久层要使用动态sql标签
        log.debug("s1-批量查询:{}",businessQuery);
        return businessRepository.getBusinessListByQuery(businessQuery);
    }

    @Override
    public void businessSave(BusinessParam businessParam) {
        //1. 根据当前业务 封装写入数据的BO(仓储层要转化)
        BusinessBO businessBO = new BusinessBO();
        BeanUtils.copyProperties(businessParam,businessBO);
        //1.1entryTime入职时间
        businessBO.setEntryTime(new Date());
        //1.2审核状态 1待审核 2审核通过 3审核驳回
        businessBO.setBusinessStatus(BusStatusEnum.APPROVED.getCode());
        //1.3调用仓储层写入
        log.debug("s2-传递数据:{}",businessBO);
        Integer id = businessRepository.save(businessBO);
        businessBO.setId(id);
        //绑定图片和商家,业务id
        bindPictures(businessBO);
    }

    @Override
    public boolean existsBusiness(Integer businessId) {
        long count=businessRepository.countBusinessById(businessId);
        return count>0;
    }

    @Override
    public BusinessBO businessDetail(Integer id) {
        return businessRepository.getBusinessById(id);
    }

    /**
     * 绑定图片和参数的方法
     * @param businessBO
     */
    private void bindPictures(BusinessBO businessBO) {
        //1. 远程调用attachApi 准备接口方法参数 绑定2张图片 营业执照 商家logo
        List<UpdateParam> attachParams=new ArrayList<>();
        String license=businessBO.getBusinessLicense();
        String logo=businessBO.getBusinessLogo();
        //1.1 封装营业执照 param
        UpdateParam paramLicense=new UpdateParam();
        paramLicense.setBusinessId(businessBO.getId());
        paramLicense.setBusinessType(100);//business type 100 shop 200 content 300
        paramLicense.setFileUuid(license.split("/")[4]);
        //1.2logo 图片参数
        UpdateParam paramlogo=new UpdateParam();
        paramlogo.setBusinessId(businessBO.getId());
        paramlogo.setBusinessType(100);//business type 100 shop 200 content 300
        paramlogo.setFileUuid(logo.split("/")[4]);
        attachParams.add(paramLicense);
        attachParams.add(paramlogo);
        //2. 调用远程接口方法
        try{
            boolean result = attachApi.batchUpdateBusiness(attachParams);
            if (!result){
                log.error("新增图片失败,检查对方接口日志");
            }
        }catch (Exception e){
            log.error("建立远程调用图片失败",e);
        }
    }
}
